<template>
  <div>
    <el-form size="mini" ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="短信模板：" prop="model">
        <el-select v-model="form.model" placeholder="请选择" style="width: 90%">
          <el-option v-for="item in modelList" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <br />
      <br />
      <br />
      <el-form-item style="text-align: center; margin-left: -100px">
        <el-button size="mini" @click="onCancel">取消</el-button>
        <el-button size="mini" :loading="loading" type="primary" @click="onSubmit">立即发送</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { smsAllModelApi } from '@/api/message'
import { apiSendMessage } from '@/api/customer'
export default {
  props: {
    id: {
      type: String,
      default: '',
    },
    phone: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      form: {
        model: '',
      },
      modelList: [],
      rules: {
        model: [{ required: true, message: '请选择短信模板', trigger: 'blur' }],
      },
      loading: false,
    }
  },
  mounted() {
    this.getModleList()
  },
  methods: {
    // 获取短信模板列表
    getModleList() {
      smsAllModelApi().then((res) => {
        const { result } = res
        this.modelList = result.map((item) => {
          return {
            value: item.sms_id,
            label: item.sms_name,
          }
        })
      })
    },
    onSubmit() {
      if (!this.phone) {
        this.$message.error('发送失败！无联系电话信息')
        return
      }
      this.$refs.form.validate((flag) => {
        if (flag) {
          this.loading = true
          apiSendMessage({
            company_id: this.id,
            sms_id: this.form.model,
            phone: this.phone,
          })
            .then((res) => {
              this.loading = false
              if (res.code === 200) {
                this.$message.success('发送成功！')
                setTimeout(() => {
                  this.$refs.form.resetFields()
                  this.$emit('close')
                }, 1000)
              }
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    onCancel() {
      this.$refs.form.resetFields()
      this.$emit('close')
    },
  },
}
</script>
<style lang="scss" scoped></style>
